package com.eascs.web.o2o.web.aging.dao;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.eascs.app.dao.JdbcSession;
import com.eascs.web.o2o.web.aging.entity.TaskNodeAuditor;
import com.eascs.web.o2o.web.aging.entity.WorkWarnEntity;

@Repository
public class TaskNodeAuditorDao {

	private static final String TASK_NODE_AUDITORS_TABLE = "ods_workflow_task_node_auditors";
	private static final String O2O_EMPLOYEE_TABLE= "ods_workflow_employee";

	@Autowired
	JdbcSession jdbcSessionBiods;
	
	public List<TaskNodeAuditor> getNodeAuditors(String taskNodeId) {
		String sql = "select * from " + TASK_NODE_AUDITORS_TABLE + " where task_node_id = ? ";
		List<TaskNodeAuditor> list = jdbcSessionBiods.findEntitys(sql, TaskNodeAuditor.class, taskNodeId);
		return list;
	}

	public List<TaskNodeAuditor> getNodeAuditorsWithDisplay(String taskNodeId) {
		String sql = "select * from " + TASK_NODE_AUDITORS_TABLE + " where task_node_id = ? " + "and is_display = '"
				+ TaskNodeAuditor.IS_DISPLAY_Y + "'";
		List<TaskNodeAuditor> list = jdbcSessionBiods.findEntitys(sql, TaskNodeAuditor.class, taskNodeId);
		return list;
	}

	public List<TaskNodeAuditor> getNodeAuditorsWithPermission(String taskNodeId) {
		String sql = "select * from " + TASK_NODE_AUDITORS_TABLE + " where task_node_id = ? and has_audit_permission = 'Y'";
		List<TaskNodeAuditor> list = jdbcSessionBiods.findEntitys(sql, TaskNodeAuditor.class, taskNodeId);
		return list;
	}

	public List<TaskNodeAuditor> getNodeAuditorsWithAdderId(String taskNodeId, String adderId) {
		String sql = "select * from " + TASK_NODE_AUDITORS_TABLE + " where task_node_id = ? and auditor_no = ?";
		List<TaskNodeAuditor> list = jdbcSessionBiods.findEntitys(sql, TaskNodeAuditor.class, taskNodeId, adderId);
		return list;
	}

	/**
	 * 
	 * @param taskNodeId
	 * @param auditorStatus
	 * @param auditorStartDate
	 * @return
	 */
	public int updateNodeAuditor(String taskNodeId, Integer auditorStatus, Date auditorStartDate) {
		String sql = "update " + TASK_NODE_AUDITORS_TABLE + " set auditor_start_date = ? , auditor_status = ? where task_node_id = ?";
		return jdbcSessionBiods.execute(sql, auditorStartDate, auditorStatus, taskNodeId);
	}

	/**
	 * 查询待办提醒
	 */
	public List<WorkWarnEntity> queryWorkWarnList() {
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT w.task_code as  taskCode,");
		sql.append(" w.node_name nodeName,");
		sql.append(" w.auditor_start_date as auditorStartDate,");
		sql.append(" w.appliedby_name as appliedbyName,");
		sql.append(" e.email ");
		sql.append(" FROM "+O2O_EMPLOYEE_TABLE+" AS e,");
		sql.append(
				" (SELECT m.task_code,n.node_name,m.appliedby_name,a.* FROM "+TASK_NODE_AUDITORS_TABLE+" a,wf_process_task_node n,wf_process_task_main m WHERE a.task_node_id=n.task_node_id AND m.task_id=n.task_id AND n.node_status=1 AND a.auditor_start_date < SUBTIME(NOW(), '24:00:00') AND a.auditor_status=0 AND a.is_display ='Y' and m.process_status != 9 ) AS w");
		sql.append(" WHERE e.employee_no=w.auditor_no");
		List<WorkWarnEntity> list = jdbcSessionBiods.findEntitys(sql.toString(), WorkWarnEntity.class,
				new Object[] {});
		return list;
	}

	public List<TaskNodeAuditor> getSignNodeAuditor(String nodeId) {
		String sql = "SELECT * from "+TASK_NODE_AUDITORS_TABLE+" t where t.auditor_type = 2 and t.task_node_id = ?";
		return jdbcSessionBiods.findEntitys(sql, TaskNodeAuditor.class, nodeId);
	}

	public int deleteNodeAuditorByNodeIds(List<String> nodeIdList) {
		String sql = "DELETE from "+TASK_NODE_AUDITORS_TABLE+" where task_node_id in ";
		StringBuffer nodeIdBuffer = new StringBuffer("(");
		for (String nodeId : nodeIdList) {
			nodeIdBuffer.append("'").append(nodeId).append("',");
		}
		String nodeIdStr = nodeIdBuffer.substring(0, nodeIdBuffer.length() - 1) + ")";
		sql = sql + nodeIdStr;
		return jdbcSessionBiods.execute(sql);
	}
}
